package code.notdo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

/**
 * author : yeswater
 * date : 2023/12/6
 * 
 * 合并 k 个升序链表
 */

public class T023 {
    public static void main(String[] args) {
        
    }

    public ListNode mergeKLists(ListNode[] lists) {
        ArrayList<Integer> list = new ArrayList<>();
        for (ListNode node : lists) {
            while (node != null) {
                list.add(node.val);
                node = node.next;
            }
        }
        list.sort(Comparator.comparingInt(a -> a));
        ListNode res = new ListNode();
        ListNode cur = res;
        for (Integer integer : list) {
            cur.next = new ListNode(integer);
            cur = cur.next;
        }
        return res.next;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
